﻿/*
 * marqdown
 * --------
 * a C# Markdown processor
 * 
 * Copyright (c) 2010 Andrew Wall
 * http://code.google.com/p/marqdown/
 * 
 * 
 */


#region Copyright and license

/*

Copyright (c) 2010 Andrew Wall

 http://www.opensource.org/licenses/mit-license.php
  
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#endregion


namespace Marqdown
{
    namespace PostProcessing
    {
        abstract class BackTickCommon : CommonState
        {
            protected void AppendMark(PostProcessor info)
            {
                info.Append("</code>");
            }
            protected string EncodeChar(char c)
            {
                switch (c)
                {
                    // Encode all ampersands; HTML entities are not
                    // entities within a Markdown code span.
                    case '&':
                        return "&amp;";
                    // Do the angle bracket song and dance
                    case '<':
                        return "&lt;";
                    case '>':
                        return "&gt;";
                    // escape characters that are magic in Markdown
                    default:
                        return "" + c;
                }
            }
        }
        class BackTick : BackTickCommon
        {
            internal static CommonState backTickText = new BackTickText();
            internal static CommonState backTick2ndTick = new BackTick2ndTick();
            internal static CommonState backTickSpace = new BackTickSpace();
            internal static CommonState backTickText2 = new BackTickText2();
            internal static CommonState backTickSpace2 = new BackTickSpace2();
            internal static CommonState backTickLastTick = new BackTickLastTick();
            public override void process(PostProcessor info, char c)
            {
                switch (c)
                {
                    case '`':
                        info.ChangeState(backTick2ndTick);
                        break;
                    default:
                        info.Append(EncodeChar(c));
                        info.ChangeState(backTickText);
                        break;
                }
            }
            public override void onEnter(PostProcessor info)
            {
                info.Append("<code>");
            }


        }
        class BackTickText : BackTickCommon
        {
            public override void process(PostProcessor info, char c)
            {
                switch (c)
                {
                    case '`':
                        AppendMark(info);
                        info.ReturnToPlain();
                        break;
                    default:
                        info.Append(EncodeChar(c));
                        break;
                }
            }
        }
        class BackTick2ndTick : BackTickCommon
        {
            public override void process(PostProcessor info, char c)
            {
                switch (c)
                {
                    case ' ':
                        info.ChangeState(BackTick.backTickSpace);
                        break;
                    default:
                        info.Append(EncodeChar(c));
                        info.ChangeState(BackTick.backTickText);
                        break;
                }
            }
        }
        class BackTickSpace : BackTickCommon
        {
            public override void process(PostProcessor info, char c)
            {
                switch (c)
                {
                    case '`':
                        info.Append(EncodeChar(c));
                        info.ChangeState(BackTick.backTickText2);
                        break;
                    default:
                        info.Append(EncodeChar(c));
                        info.ChangeState(BackTick.backTickText);
                        break;
                }
            }
        }
        class BackTickText2 : BackTickCommon
        {
            public override void process(PostProcessor info, char c)
            {
                switch (c)
                {
                    case ' ':
                        info.ChangeState(BackTick.backTickSpace2);
                        break;
                    default:
                        info.Append(EncodeChar(c));
                        break;
                }
            }
        }
        class BackTickSpace2 : BackTickCommon
        {
            public override void process(PostProcessor info, char c)
            {
                switch (c)
                {
                    case '`':
                        info.ChangeState(BackTick.backTickLastTick);
                        break;
                    default:
                        info.Append(EncodeChar(' '));
                        info.Append(EncodeChar(c));
                        info.ChangeState(BackTick.backTickText2);
                        break;
                }
            }
        }
        class BackTickLastTick : BackTickCommon
        {
            public override void process(PostProcessor info, char c)
            {
                switch (c)
                {
                    case '`':
                        AppendMark(info);
                        info.ReturnToPlain();
                        break;
                    default:
                        info.Append(EncodeChar(' '));
                        info.Append(EncodeChar('`'));
                        info.Append(EncodeChar(c));
                        info.ChangeState(BackTick.backTickText2);
                        break;
                }
            }
        }



    }
}